Imports System.ComponentModel
Imports System.Drawing.Drawing2D
Imports System.Windows.Forms

Imports Phoenix.Forms.Login.Helpers

Namespace Forms.Login

    Public Class XPTextBox
        Inherits System.Windows.Forms.UserControl

        Private mHasFocus As Boolean = False
        Private mBackColor As Color = SystemColors.Window
        Private mBorderColor As Color = SystemColors.ActiveBorder
        Private mForeColor As Color = SystemColors.WindowText

#Region " Windows Form Designer generated code "

        Public Sub New()
            MyBase.New()

            'This call is required by the Windows Form Designer.
            InitializeComponent()

            'Add any initialization after the InitializeComponent() call
            SetStyle(ControlStyles.ResizeRedraw, True)
            SetStyle(ControlStyles.AllPaintingInWmPaint, True)
            SetStyle(ControlStyles.UserPaint, True)
            SetStyle(ControlStyles.DoubleBuffer, True)
            SetStyle(ControlStyles.SupportsTransparentBackColor, True)
            SetStyle(ControlStyles.ContainerControl, True)

            MyBase.BackColor = Color.Transparent
        End Sub

        'UserControl overrides dispose to clean up the component list.
        Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
            If disposing Then
                If Not (components Is Nothing) Then
                    components.Dispose()
                End If
            End If
            MyBase.Dispose(disposing)
        End Sub

        'Required by the Windows Form Designer
        Private components As System.ComponentModel.IContainer

        'NOTE: The following procedure is required by the Windows Form Designer
        'It can be modified using the Windows Form Designer.  
        'Do not modify it using the code editor.
        Private WithEvents MyTextBox As System.Windows.Forms.TextBox
        <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
            Me.MyTextBox = New System.Windows.Forms.TextBox
            Me.SuspendLayout()
            '
            'MyTextBox
            '
            Me.MyTextBox.BorderStyle = System.Windows.Forms.BorderStyle.None
            Me.MyTextBox.Location = New System.Drawing.Point(24, 8)
            Me.MyTextBox.Name = "MyTextBox"
            Me.MyTextBox.TabIndex = 0
            Me.MyTextBox.Text = ""
            '
            'XPTextBox
            '
            Me.Controls.Add(Me.MyTextBox)
            Me.Name = "XPTextBox"
            Me.ResumeLayout(False)

        End Sub

#End Region

#Region "Custom Painting"
        Protected Overrides Sub OnPaint(ByVal e As System.Windows.Forms.PaintEventArgs)
            MyBase.OnPaint(e)
            MyBase.BackColor = Me.Parent.BackColor

            e.Graphics.SmoothingMode = SmoothingMode.HighQuality

            PositionTextBox()

            Dim lpenBorder As New Pen(mBorderColor, 1)
            Dim gp As New GraphicsPath

            lpenBorder.Alignment = PenAlignment.Inset
            gp = Paths.RoundedRect(New Rectangle(0, 0, Me.Width, Me.Height), 3, 1, Corner.All)

            If Me.Enabled Then
                e.Graphics.FillPath(New SolidBrush(mBackColor), gp)
                MyTextBox.BackColor = mBackColor
                If mHasFocus Then
                    e.Graphics.DrawLine(New Pen(Color.Orange, 2), 1, Me.Height - 2, Me.Width - 2, Me.Height - 2)
                End If
                e.Graphics.DrawPath(lpenBorder, gp)
            Else
                MyTextBox.BackColor = CtrlHelper.GetNonTransparentBackColor(Me)
                e.Graphics.FillPath(New SolidBrush(Me.Parent.BackColor), gp)
                e.Graphics.DrawPath(lpenBorder, gp)
            End If

            gp.Dispose()
            lpenBorder.Dispose()
        End Sub
#End Region

#Region "Public Properties"
        <Browsable(True), _
        Description("The Backgroundcolor"), _
        Category("Appearance"), _
        DefaultValue(GetType(Color), "Window"), _
        DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)> _
        Public Shadows Property BackColor() As Color
            Get
                Return mBackColor
            End Get
            Set(ByVal value As Color)
                mBackColor = value
                MyTextBox.BackColor = value
                Me.Invalidate()
            End Set
        End Property

        <Browsable(True), _
        Description("the border color for the textbox"), _
        Category("Appearance"), _
        DefaultValue(GetType(Color), "ActiveBorder"), _
        DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)> _
        Public Property BorderColor() As Color
            Get
                Return mBorderColor
            End Get
            Set(ByVal value As Color)
                mBorderColor = value
                Me.Invalidate()
            End Set
        End Property

        <Browsable(True), _
        Description("the font of the textbox"), _
        Category("Appearance"), _
        DefaultValue(GetType(Font), "Microsoft Sans Serif, 8.25pt"), _
        DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)> _
        Public Overrides Property Font() As Font
            Get
                Return MyBase.Font
            End Get
            Set(ByVal value As Font)
                MyBase.Font = value
                MyTextBox.Font = value
                MyTextBox.Invalidate()
                Me.Invalidate()
            End Set
        End Property

        <Browsable(True), _
        Description("show multiple lines of text in the control"), _
        Category("Behavior"), _
        DefaultValue(False), _
        DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)> _
        Public Property Multiline() As Boolean
            Get
                Return MyTextBox.Multiline
            End Get
            Set(ByVal value As Boolean)
                MyTextBox.Multiline = value
                Me.Invalidate()
            End Set
        End Property

        <Browsable(True), _
        Description("the forecolor (font color) of the control"), _
        Category("Appearance"), _
        DefaultValue(GetType(Color), "WindowText"), _
        DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)> _
        Public Overrides Property ForeColor() As Color
            Get
                Return mForeColor
            End Get
            Set(ByVal value As Color)
                mForeColor = value
                MyTextBox.ForeColor = value
                Me.Invalidate()
            End Set
        End Property

        <Browsable(True), _
        Localizable(True), _
        Description("the text to display in the control"), _
        Category("Appearance"), _
        DefaultValue(""), _
        DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)> _
        Public Overrides Property Text() As String
            Get
                Return MyTextBox.Text
            End Get
            Set(ByVal value As String)
                MyTextBox.Text = value
                Me.Invalidate()
            End Set
        End Property

        <Browsable(True), _
        Localizable(True), _
        Description("checks if the control is enabled or disabled"), _
        Category("Appearance"), _
        DefaultValue(True)> _
        Public Shadows Property Enabled() As Boolean
            Get
                Return MyBase.Enabled
            End Get
            Set(ByVal value As Boolean)
                MyBase.Enabled = value
                Me.Invalidate()
            End Set
        End Property

        <Browsable(True), _
        Localizable(True), _
        Description("sets the character that is displayed instead of the real typed char"), _
        Category("Appearance"), _
        DefaultValue("")> _
        Public Property PasswortChar() As Char
            Get
                Return MyTextBox.PasswordChar
            End Get
            Set(ByVal value As Char)
                MyTextBox.PasswordChar = value
            End Set
        End Property
#End Region

#Region "private methods"
        Protected Overrides Sub OnResize(ByVal e As System.EventArgs)
            MyBase.OnResize(e)
            PositionTextBox()
        End Sub

        Private Sub PositionTextBox()
            With MyTextBox
                .Left = 5
                .Top = 5
                .Width = Me.Width - 10
                If .Multiline Then
                    .Height = Me.Height - 10
                Else
                    Me.Height = .Height + 10
                End If
            End With
        End Sub

#End Region

#Region "tb events to bubble up"
        Public Shadows Event TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)
        Public Shadows Event Validated(ByVal sender As System.Object, ByVal e As System.EventArgs)
        Public Shadows Event Validating(ByVal sender As System.Object, ByVal e As System.EventArgs)
        Public Shadows Event KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs)
        Public Shadows Event KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs)
        Public Shadows Event KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs)
        Public Shadows Event GotFocus(ByVal sender As System.Object, ByVal e As System.EventArgs)
        Public Shadows Event LostFocus(ByVal sender As System.Object, ByVal e As System.EventArgs)
        Public Shadows Event ParentChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)

        Private Sub MyTextBox_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyTextBox.TextChanged
            RaiseEvent TextChanged(Me, e)
        End Sub

        Private Sub MyTextBox_Validated(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyTextBox.Validated
            RaiseEvent Validated(Me, e)
        End Sub

        Private Sub MyTextBox_Validating(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles MyTextBox.Validating
            RaiseEvent Validating(Me, e)
        End Sub

        Private Sub MyTextBox_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles MyTextBox.KeyDown
            RaiseEvent KeyDown(Me, e)
        End Sub

        Private Sub MyTextBox_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles MyTextBox.KeyPress
            RaiseEvent KeyPress(Me, e)
        End Sub

        Private Sub MyTextBox_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles MyTextBox.KeyUp
            RaiseEvent KeyUp(Me, e)
        End Sub

        Private Sub MyTextBox_GotFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyTextBox.GotFocus
            mHasFocus = True
            Me.Invalidate()

        End Sub

        Private Sub MyTextBox_LostFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyTextBox.LostFocus
            mHasFocus = False
            Me.Invalidate()
        End Sub

        Private Sub XPTextBox_ParentChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.ParentChanged
            Me.Invalidate()
        End Sub
#End Region
    End Class

End Namespace

